package com.itydl.bounce.view;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.RelativeLayout;

/**
 * 作者：${Howard} on 2018/2/5 14:47
 *
 * @version 1.0
 *          说明：加载View
 */
public class LoadingView extends RelativeLayout {

    private CircularView mLeftCircularView;
    private CircularView mCenterCircularView;
    private CircularView mRightCircularView;

    private int translationDis = 30;
    private long DURATION = 500;
    private boolean isViewGone = false;//动画是否应该是结束状态标志

    public LoadingView(Context context) {
        this(context,null);
    }

    public LoadingView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public LoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
        translationDis = dip2px(30);
    }

    private void init(Context context) {
        mLeftCircularView = createCircularView(context);
        mLeftCircularView.setColor(Color.RED);

        mRightCircularView = createCircularView(context);
        mRightCircularView.setColor(Color.BLUE);

        mCenterCircularView = createCircularView(context);
        mCenterCircularView.setColor(Color.GREEN);

        addView(mLeftCircularView);
        addView(mRightCircularView);
        //后添加在上面，表示中间的小圆
        addView(mCenterCircularView);

        /**---------  开启动画  ---------**/
        post(new Runnable() {
            @Override
            public void run() {
                startOutAnimation();
            }
        });
    }

    /**
     * 开启往外移动的动画
     */
    private void startOutAnimation() {
        if(isViewGone){
            return;
        }
        //左边小圆左移动画
        ObjectAnimator outLeftObjectAnimator = ObjectAnimator.ofFloat(mLeftCircularView,"TranslationX",0f,-translationDis);
        //右边小圆右移动画
        ObjectAnimator outRightObjectAnimator = ObjectAnimator.ofFloat(mRightCircularView,"TranslationX",0f,translationDis);

        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(DURATION);
        animatorSet.setInterpolator(new DecelerateInterpolator(2f));
        animatorSet.playTogether(outLeftObjectAnimator,outRightObjectAnimator);

        animatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                //动画结束，开启往内移动的动画
                startInnerAnimation();
            }
        });
        animatorSet.start();
    }

    /**
     * 开启往内移动的动画
     */
    private void startInnerAnimation() {
        if(isViewGone){
            return;
        }

        //左边小圆右移动画
        ObjectAnimator innerLeftObjectAnimator = ObjectAnimator.ofFloat(mLeftCircularView,"TranslationX",-translationDis,0f);
        //右边小圆左移动画
        ObjectAnimator innerRightObjectAnimator = ObjectAnimator.ofFloat(mRightCircularView,"TranslationX",translationDis,0f);

        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(DURATION);
        animatorSet.setInterpolator(new AccelerateInterpolator());
        animatorSet.playTogether(innerLeftObjectAnimator,innerRightObjectAnimator);

        animatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                //动画结束，开启往内移动的动画
                startOutAnimation();

                //改变小球颜色
                //左边小球颜色—>中间小球颜色—>右边小球颜色—>左边小球颜色
                int mLeftCircularViewColor = mLeftCircularView.getColor();
                int mCenterCircularViewColor = mCenterCircularView.getColor();
                int mRightCircularViewColor = mRightCircularView.getColor();

                mCenterCircularView.setColor(mLeftCircularViewColor);
                mRightCircularView.setColor(mCenterCircularViewColor);
                mLeftCircularView.setColor(mRightCircularViewColor);
            }
        });
        animatorSet.start();
    }

    /**
     * 统一创建小圆
     * @param context
     * @return
     */
    private CircularView createCircularView(Context context) {
        CircularView circularView = new CircularView(context);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(dip2px(10),dip2px(10));
        params.addRule(CENTER_IN_PARENT);
        circularView.setLayoutParams(params);
        return circularView;
    }

    /**
     * 清空动画,清空View
     * @param visibility
     */
    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(View.INVISIBLE);
        mLeftCircularView.clearAnimation();
        mRightCircularView.clearAnimation();

        ViewGroup parent = (ViewGroup) getParent();
        if(parent != null){
            //因为自己装到了父View中了
            parent.removeView(this);
            //移除自己的Views
            removeAllViews();
        }
        isViewGone = true;
    }

    private int dip2px(int dip) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dip,getResources().getDisplayMetrics());
    }
}
